package com.hangyutech.strelitzia.modules.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.hangyutech.strelitzia.modules.entity.ArticlesExtend;
import com.hangyutech.strelitzia.modules.entity.Articles;
import com.hangyutech.strelitzia.modules.service.ArticlesService;
import com.hangyutech.strelitzia.vo.HttpResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 文章总表(Articles)表控制层
 *
 * @author makejava
 * @since 2020-12-16 16:19:05
 */
@RestController
@RequestMapping("articles")
@CrossOrigin
@Api(description = "文章")
public class ArticlesController {
    /**
     * 服务对象
     */
    @Resource
    private ArticlesService articlesService;


//    @Autowired
//    private OssClientFactory ossClientFactory;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据id获取")
    @GetMapping("rows/{id}")
    public HttpResult selectOne(
            @ApiParam(name = "id", value = "id", required = true)
            @PathVariable Integer id) {
        JSONObject row = this.articlesService.queryContentById(id);
        if (Objects.isNull(row)) {
            return HttpResult.error().message("未找到内容");
        } else {
            return HttpResult.ok()
                    .data(row)
                    .message("查询列表成功");

        }
    }


    @ApiOperation(value = "增加新记录")
    @RequestMapping(value = "rows", method = RequestMethod.POST)
    public HttpResult insert(
            @RequestParam(value = "type", required = false, defaultValue = "1") Integer type,
            @RequestParam(value = "cid", required = false, defaultValue = "0") Integer cid,
            @RequestParam(value = "title", required = false, defaultValue = "") String title,
            @RequestParam(value = "short_title", required = false, defaultValue = "") String short_title,
            @RequestParam(value = "description", required = false, defaultValue = "") String description,
            @RequestParam(value = "tags", required = false, defaultValue = "") String tags,
            @RequestParam(value = "cover", required = false, defaultValue = "") String cover,
            @RequestParam(value = "thumbnail", required = false, defaultValue = "") String thumbnail,
            @RequestParam(value = "author_id", required = false, defaultValue = "0") Integer author_id,
            @RequestParam(value = "source", required = false, defaultValue = "") String source,
            @RequestParam(value = "video_url", required = false, defaultValue = "") String video_url,
            @RequestParam(value = "voice_url", required = false, defaultValue = "") String voice_url,
            @RequestParam(value = "weight", required = false, defaultValue = "0") Integer weight,
            @RequestParam(value = "status", required = false, defaultValue = "0") Integer status,
            @RequestParam(value = "visible", required = false, defaultValue = "0") Integer visible,
            @RequestParam(value = "view_count", required = false, defaultValue = "0") Integer view_count,
            @RequestParam(value = "like_count", required = false, defaultValue = "0") Integer like_count,
            @RequestParam(value = "comment_count", required = false, defaultValue = "0") Integer comment_count,
            @RequestParam(value = "content", required = false, defaultValue = "") String content
    ) {

//        if (Objects.isNull(
//                redisUtil.get(
//                        redisUtil.redisPrefix + ":token:" + request.getHeader("Authorization")
//                )
//        )) {
//            return HttpResult.error().message("身份验证失败，请重新登陆");
//        }
        Articles article = new Articles();
        article.setType(type);
        article.setCid(cid);
        article.setTitle(title);
        article.setShortTitle(short_title);
        article.setDescription(description);
        article.setTags(tags);
        article.setCover(cover);
        article.setThumbnail(thumbnail);
        article.setAuthorId(author_id);
        article.setSource(source);
        article.setVideoUrl(video_url);
        article.setVoiceUrl(voice_url);
        article.setWeight(weight);
        article.setStatus(status);
        article.setVisible(visible);
        article.setViewCount(view_count);
        article.setLikeCount(like_count);
        article.setCommentCount(comment_count);
        ArticlesExtend articlesExtend = new ArticlesExtend();
        articlesExtend.setContent(content);
        Articles data = articlesService.insert(article, articlesExtend);
        return Objects.isNull(data) ? HttpResult.error() : HttpResult.ok();
    }


    @ApiOperation(value = "根据id修改记录")
    @RequestMapping(value = "rows/{id}", method = RequestMethod.PUT)
    public HttpResult update(
            @PathVariable Integer id,
            @RequestParam(value = "type", required = false, defaultValue = "1") Integer type,
            @RequestParam(value = "cid", required = false, defaultValue = "0") Integer cid,
            @RequestParam(value = "title", required = false, defaultValue = "") String title,
            @RequestParam(value = "short_title", required = false, defaultValue = "") String short_title,
            @RequestParam(value = "description", required = false, defaultValue = "") String description,
            @RequestParam(value = "tags", required = false, defaultValue = "") String tags,
            @RequestParam(value = "cover", required = false, defaultValue = "") String cover,
            @RequestParam(value = "thumbnail", required = false, defaultValue = "") String thumbnail,
            @RequestParam(value = "author_id", required = false, defaultValue = "0") Integer author_id,
            @RequestParam(value = "source", required = false, defaultValue = "") String source,
            @RequestParam(value = "video_url", required = false, defaultValue = "") String video_url,
            @RequestParam(value = "voice_url", required = false, defaultValue = "") String voice_url,
            @RequestParam(value = "weight", required = false, defaultValue = "0") Integer weight,
            @RequestParam(value = "status", required = false, defaultValue = "0") Integer status,
            @RequestParam(value = "visible", required = false, defaultValue = "0") Integer visible,
            @RequestParam(value = "view_count", required = false, defaultValue = "0") Integer view_count,
            @RequestParam(value = "like_count", required = false, defaultValue = "0") Integer like_count,
            @RequestParam(value = "comment_count", required = false, defaultValue = "0") Integer comment_count,
            @RequestParam(value = "content", required = false, defaultValue = "") String content
    ) {


//        if (Objects.isNull(
//                redisUtil.get(
//                        redisUtil.redisPrefix + ":token:" + request.getHeader("Authorization")
//                )
//        )) {
//            return HttpResult.error().message("身份验证失败，请重新登陆");
//        }
        Articles article = this.articlesService.queryById(id);
        if (!type.equals(article.getType())) article.setType(type);
        if (!cid.equals(article.getCid())) article.setCid(cid);
        if (!title.equals(article.getTitle())) article.setTitle(title);
        if (!short_title.equals(article.getShortTitle())) article.setShortTitle(short_title);
        if (!description.equals(article.getDescription())) article.setDescription(description);
        if (!tags.equals(article.getTags())) article.setTags(tags);
        if (!cover.equals(article.getCover())) article.setCover(cover);
        if (!thumbnail.equals(article.getThumbnail())) article.setThumbnail(thumbnail);
        if (!author_id.equals(article.getAuthorId())) article.setAuthorId(author_id);
        if (!source.equals(article.getSource())) article.setSource(source);
        if (!video_url.equals(article.getVideoUrl())) article.setVideoUrl(video_url);
        if (!voice_url.equals(article.getVoiceUrl())) article.setVoiceUrl(voice_url);
        if (!weight.equals(article.getWeight())) article.setWeight(weight);
        if (!status.equals(article.getStatus())) article.setStatus(status);
        if (!visible.equals(article.getVisible())) article.setVisible(visible);
        if (!view_count.equals(article.getViewCount())) article.setViewCount(view_count);
        if (!like_count.equals(article.getLikeCount())) article.setLikeCount(like_count);
        if (!comment_count.equals(article.getCommentCount())) article.setCommentCount(comment_count);
        ArticlesExtend articleExtend = null;
        if (!content.equals("")) {
            articleExtend = new ArticlesExtend();
            articleExtend.setAid(article.getId());
            articleExtend.setContent(content);
        }
        Articles data = articlesService.update(article, articleExtend);
        return Objects.isNull(data) ? HttpResult.error() : HttpResult.ok();
    }


    @GetMapping("rows")
    @ApiOperation(value = "分页查看列表", notes = "分页查看列表 (status  -1全部检索 0正常 1禁用 2需审核)")
    public HttpResult search(
            @RequestParam("pageIndex") int pageIndex,
            @RequestParam("pageSize") int pageSize,
            @RequestParam(value = "status", required = false, defaultValue = "-1") int status) {
        PageInfo<Articles> rowList = articlesService.search(pageIndex, pageSize, status, "");

//        return HttpResult.ok();
//        List<Articles> rowList = articlesService.queryAllByLimit(pageIndex,pageSize);
        return HttpResult.ok()
                .data(rowList)
                .message("查询列表成功");
    }

    @ApiOperation(value = "根据id删除")
    @DeleteMapping("rows/{id}")
    public HttpResult delete(
            @ApiParam(name = "id", value = "id", required = true)
            @PathVariable Integer id) {

//        if (Objects.isNull(
//                redisUtil.get(
//                        redisUtil.redisPrefix + ":token:" + request.getHeader("Authorization")
//                )
//        )) {
//            return HttpResult.error().message("身份验证失败，请重新登陆");
//        }

        articlesService.deleteById(id);
        return HttpResult.ok();
    }

}